package santeclair.referentiel.webservice.rest.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import santeclair.referentiel.bean.Civilite;
import santeclair.referentiel.dto.CiviliteDto;
import santeclair.referentiel.service.CiviliteService;
import santeclair.referentiel.webservice.CiviliteWebService;

import com.google.common.annotations.VisibleForTesting;

@Service("civiliteWebService")
public class CiviliteWebServiceImpl implements CiviliteWebService {

    @Autowired
    @Qualifier("civiliteService")
    private CiviliteService civiliteService;

    /**
     * {@inheritDoc}
     */
    @Override
    public List<CiviliteDto> listerCivilites() {
        List<CiviliteDto> result = new ArrayList<CiviliteDto>();
        for (Civilite civilite : civiliteService.findAll()) {
            CiviliteDto dto = new CiviliteDto();
            dto.setAbreviation(civilite.getAbreviation());
            dto.setCode(civilite.getCode());
            dto.setLibelle(civilite.getLibelle());
            result.add(dto);
        }
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public CiviliteDto getCivilite(String codeCivilite) {
        Civilite civilite = civiliteService.getByCode(codeCivilite);
        CiviliteDto dto = new CiviliteDto();
        dto.setAbreviation(civilite.getAbreviation());
        dto.setCode(civilite.getCode());
        dto.setLibelle(civilite.getLibelle());

        return dto;
    }

    /* ======================================================= *
     *                      getters & setters 
     * ======================================================= */

    /**
     * @param civiliteService the civiliteService to set
     */
    @VisibleForTesting
    public void setCiviliteService(CiviliteService civiliteService) {
        this.civiliteService = civiliteService;
    }

}
